<template>
  <div class="app-container">
    <div class="set-box">
      <div class="website-title">支付宝支付设置</div>
      <div class="filter-container">
        <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-plus" round @click="createDialog = true;isadd = true">
          新增
        </el-button>
      </div>
      <el-table
        :key="tableKey"
        v-loading="listLoading"
        :data="list"
        fit
        highlight-current-row
        style="width: 100%;"
      >
        <el-table-column label="支付类型">
          <template>
            <span>alipay</span>
          </template>
        </el-table-column>
        <el-table-column label="appid" show-overflow-tooltip>
          <template>
            <span>2021003127685692</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" show-overflow-tooltip>
          <template>
            <span>
              支付宝支付
            </span>
          </template>
        </el-table-column>
        <el-table-column label="状态">
          <template>
            <el-switch v-model="value" active-text="启用" inactive-text="禁用" />
          </template>
        </el-table-column>
        <el-table-column label="操作" class-name="small-padding fixed-width">
          <template>
            <i class="el-icon-delete danger-color" @click="handleDelete" />
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <!-- 展示新增对话框 -->
    <el-dialog
      :title="isadd?'新增支付宝支付设置':'编辑支付宝支付设置'"
      :visible.sync="createDialog"
      width="50%"
      custom-class="operate-dialog"
      top="20vh"
      center
    >
      <el-form :model="oprateform" label-width="120px">
        <el-form-item label="app_id">
          <el-input v-model="oprateform.name" placeholder="必填-支付宝分配的 app_id" />
        </el-form-item>
        <el-form-item label="应用私钥">
          <el-input v-model="oprateform.name" placeholder="必填-应用私钥 字符串或路径" />
        </el-form-item>
        <el-form-item label="应用公钥证书">
          <el-input v-model="oprateform.name" placeholder="必填-应用公钥证书 路径" />
          <el-upload
            ref="upload"
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            :auto-upload="false"
          >
            <el-button slot="trigger" type="primary" icon="el-icon-upload2">上传证书</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="支付宝公钥证书">
          <el-input v-model="oprateform.name" placeholder="必填-支付宝公钥证书 路径" />
          <el-upload
            ref="upload"
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            :auto-upload="false"
          >
            <el-button slot="trigger" type="primary" icon="el-icon-upload2">上传证书</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="支付宝根证书">
          <el-input v-model="oprateform.name" placeholder="必填-支付宝根证书 路径" />
          <el-upload
            ref="upload"
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            :auto-upload="false"
          >
            <el-button slot="trigger" type="primary" icon="el-icon-upload2">上传证书</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="oprateform.describe" type="textarea" :autosize="{ minRows: 1, maxRows: 4}" placeholder="必填- 这配置的说明，方便了解" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleResetOperate">重置</el-button>
        <el-button type="primary" @click="handleOperate">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { fetchList } from '@/api/article'
import Pagination from '@/components/Pagination'
export default {
  name: 'Adminrole',
  components: { Pagination },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        title: ''
      },
      value: false,
      oprateform: { // 新增、编辑条件
        name: '',
        describe: ''
      },
      isadd: true, // 是新增 否编辑
      createDialog: false, // 是否展示新增、编辑对话框
      fileList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.items
        this.total = response.data.total

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 1.5 * 1000)
      })
    },
    // 新增、编辑
    handleOperate() {
      this.createDialog = false
    },
    // 重置新增、编辑支付宝支付配置输入框
    handleResetOperate() {
      this.oprateform.name = ''
      this.oprateform.describe = ''
    },
    // 删除
    handleDelete(row, index) {
      this.$confirm('确定要删除该支付宝支付配置？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        center: true,
        customClass: 'text-center'
      }).then(() => {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success',
          duration: 2000
        })
        // this.$message({
        //   type: 'success',
        //   message: '删除成功!'
        // })
      }).catch(() => {

      })
      this.list.splice(index, 1)
    },
    submitUpload() {
      this.$refs.upload.submit()
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePreview(file) {
      console.log(file)
    }
  }
}
</script>
<style scoped lang="scss">
  [class^=el-icon-], [class*=" el-icon-"]{
    font-size: 16px;
  }
  ::v-deep .el-form--inline .el-form-item{
    width: calc(50% - 100px);
    margin-right: 100px;
    &:nth-child(even){
      margin-right: 0;
    }
    &:last-child{
    width: calc(100% - 100px);
  }
  }
  ::v-deep .el-form-item--medium .el-form-item__content{
    width: calc(100% - 100px);
  }
  ::v-deep .el-input--medium .el-input__inner{
    height: 40px;
    line-height: 40px;
  }
  ::v-deep .el-form-item__label{
    color: #303133;
    font-weight: normal;
    line-height: 40px;
  }
  ::v-deep .set-box{
    .el-form-item__label{
       line-height: 22px;
    }
    .el-form-item{
      display: flex;
      align-items: center;
      width: calc(100% - 100px);
    }
   .el-form-item--medium .el-form-item__content{
    margin-left: 0!important;
   }
  }
  .website-title{
    font-weight: 700;
    color: #191919;
    font-size: 20px;
    font-family: Source Han Sans CN-Bold, Source Han Sans CN;
    padding-bottom: 20px;
  }
  ::v-deep .el-upload .el-button{
    margin-top: 10px;
  }
</style>
